//
//  ZYMainController.m
//  Joynet
//
//  Created by admin on 2017/7/24.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ZYMainController.h"
#import "WYBaseNavigationController.h"
#import "WYTabBar.h"

#import "WYMapController.h"

#import "WYBusinessController.h"

#import "WYCityController.h"

#import "WYPersonController.h"

#import "WYReleaseView.h"

#import "WYSendRedPacketController.h"
#import "WYLoginController.h"
#import "WYDoneMessageController.h"
#import "CJAssetsController.h"
@interface ZYMainController ()<WYReleaseViewDelegate,UITabBarControllerDelegate>
@property (nonatomic, strong) WYTabBar *wyTabbar;
@property (nonatomic,strong)WYReleaseView *releaseView;
@property (nonatomic,strong) UIViewController *pvc;
@property (nonatomic,strong)UIButton *centerBtn;
@property (nonatomic,assign) CGFloat itemState;
@end

@implementation ZYMainController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setupAllChildViewControllers];
//     self.tabBar.barTintColor=[UIColor hexStringToColor:@"ffffff" alpha:1.0];
    // Do any additional setup after loading the view.
    [self setupAllChildViewControllers];
    [self setCustomtabbar];
    self.delegate = self;
}
//-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//
//    if (tabBarController.selectedIndex==3) {
//        NSString *token =[WYStoreModule currentToken];
//        if (token.length) {
////            return YES;
//        }else{
//            WYLoginController *loginVc = [[WYLoginController alloc] initWithNibName:@"WYLoginController" bundle:nil];
//            WYBaseNavigationController *nav = [[WYBaseNavigationController alloc]initWithRootViewController:loginVc];
//            [self presentViewController:nav animated:YES completion:nil];
////            return NO;
//        }
//    }
//
//    NSLog(@"tabBarController = %ld",tabBarController.selectedIndex);
//
//
//}
//-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
//{
//
//}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([viewController.tabBarItem.title isEqualToString:@"我的"]) {
        NSString *token =[WYStoreModule currentToken];
        if (token.length) {
            return YES;
        }else{
            WYLoginController *loginVc = [[WYLoginController alloc] initWithNibName:@"WYLoginController" bundle:nil];
            WYBaseNavigationController *nav = [[WYBaseNavigationController alloc]initWithRootViewController:loginVc];
            [self presentViewController:nav animated:YES completion:nil];
            return NO;
        }
    }

    return YES;




}

#pragma mark - 设置自定义中心按钮
- (void)setCustomtabbar{
    WYTabBar *tabbar = [[WYTabBar alloc]init];
    [self setValue:tabbar forKeyPath:@"tabBar"];
    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)centerBtnClick:(UIButton *)btn{
    btn.selected = YES;
    self.centerBtn = btn;
    UIViewController *pvc = [self getCurrentVC];
    self.pvc = pvc;
    
    //UIImage *image = [self nomalSnapshotImage];
    WYReleaseView *releaseeView =  [[NSBundle mainBundle] loadNibNamed:@"WYReleaseView" owner:nil options:nil].firstObject;
    //releaseeView.backImage.image = image;
    self.releaseView = releaseeView;
    releaseeView.delegate = self;
    releaseeView.frame = [UIApplication sharedApplication].delegate.window.frame;
//    releaseeView.backgroundColor = [UIColor hexStringToColor:@"#e9e5e1" alpha:0.9];
    releaseeView.backgroundColor = [UIColor hexStringToColor:@"#ffffff" alpha:0.9];
     [[UIApplication sharedApplication].keyWindow addSubview:releaseeView];
   
//    WYReleaseView *releaseView = [[WYReleaseView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    releaseView.backgroundColor = [UIColor hexStringToColor:@"#ffffff" alpha:0.3];
//    UIViewController *currentVc = [self getCurrentVC];
//    [currentVc.view addSubview:releaseView];
//    [[UIApplication sharedApplication].keyWindow addSubview:releaseView];
    NSLog(@"dasd");
    
    
    
   
    
    
    
    
    
}

- (UIImage *)nomalSnapshotImage
{
    UIGraphicsBeginImageContextWithOptions(self.pvc.view.size, NO, [UIScreen mainScreen].scale);
    [self.pvc.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return snapshotImage;
}

-(void)clickCityView
{
    
    MBProgressHUD *_mbp = [[MBProgressHUD alloc] initWithView:[APPDELEGATE window]];
    //_mbp.opacity = 0.6f;
    _mbp.tag = 99999;
    _mbp.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _mbp.bezelView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4];
    _mbp.contentColor = [UIColor whiteColor];
    [[APPDELEGATE window] addSubview:_mbp];


    _mbp.mode = MBProgressHUDModeText;
    //    _mbp.delegate = [[[UIApplication sharedApplication] delegate] window];
    //_mbp.labelText = text;
    _mbp.detailsLabel.text = @"暂未开通";
    _mbp.margin = 15.f;
    _mbp.detailsLabel.font = kFontSystem(16);
    _mbp.userInteractionEnabled = NO;
    _mbp.minShowTime = 2.0;
    [[APPDELEGATE window] bringSubviewToFront:_mbp];
    [_mbp showAnimated:YES];
    [_mbp hideAnimated:YES afterDelay:1];
    
    
//    [self.releaseView removeFromSuperview];
//    UIViewController *vc = [[UIViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
}
-(void)clickRedView
{
    
    self.centerBtn.selected = NO;
    [self.releaseView removeFromSuperview];
    
    if (![self isLogin]) {
        return;
    }
    
   
    WYSendRedPacketController *vc = [[WYSendRedPacketController alloc]init];
    UIViewController *pvc =[self getCurrentVC];
    [pvc.navigationController pushViewController:vc animated:YES];
}

- (void)clickBackView {
    self.centerBtn.selected = NO;
}


//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}
- (void)setupAllChildViewControllers {
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor hexStringToColor:@"#999999" alpha:1.0], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    
    UIColor *titleHighlightedColor = kColorMainColor;
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
    WYMapController *vc=[[WYMapController alloc]init];
    [self setupChildViewController:vc title:@"BOSS" imageName:@"BOSS_normal" selectedImageName:@"BOSS_selected"];
    WYBusinessController *vc1=[[WYBusinessController alloc]init];
    [self setupChildViewController:vc1 title:@"商机" imageName:@"business_normal" selectedImageName:@"business_selected"];
//    WYDoneMessageController *vc2=[[WYDoneMessageController alloc]init];
//    [self setupChildViewController:vc2 title:@"同城" imageName:@"city_normal" selectedImageName:@"city_selected"];
    
    
    CJAssetsController *vc2=[[CJAssetsController alloc]init];
    [self setupChildViewController:vc2 title:@"资产" imageName:@"tabbar_asset_normal" selectedImageName:@"tabbar_asset_selected"];
    
    
    
    WYPersonController *vc3=[[WYPersonController alloc]init];
    [self setupChildViewController:vc3 title:@"我的" imageName:@"me_normal" selectedImageName:@"me_selected"];
}
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    childVc.tabBarItem.title=title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selectedIamge = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.selectedImage = [selectedIamge imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // CGFloat offset = 5.0;
    //tabBar图片居中显示，不显示文字
    //    childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
    WYBaseNavigationController *navVc = [[WYBaseNavigationController alloc] initWithRootViewController:childVc];
    //    navVc.navigationBar.barTintColor=[UIColor hexStringToColor:@"#d8593f" alpha:1.0];
    
    [self addChildViewController:navVc];
}
-(BOOL)isLogin
{
    NSString *token =[WYStoreModule currentToken];
    if (token.length) {
        return YES;
    }else{
        WYLoginController *loginVc = [[WYLoginController alloc] initWithNibName:@"WYLoginController" bundle:nil];
        WYBaseNavigationController *nav = [[WYBaseNavigationController alloc]initWithRootViewController:loginVc];
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }
    
}
@end
